import Vue from 'vue'
import VueRouter from 'vue-router'
import Teacher from '../views/Teacher.vue'
import Student from '../views/Student'
import Login from '../views/Login.vue'
import TeacherIndex from '../views/teacher/TeacherIndex.vue'
import StudentIndex from '../views/student/StudentIndex.vue'
import store from "../store"
import settings from "@/assets/js/settings";
import cookies from "vue-cookies";
import axios from "axios";


Vue.use(VueRouter)

const routes = [
    {
        path: '/teacher',
        name: 'teacher',
        component: Teacher,
        children: [ // 教师子路由
            {
                path: '/TeacherIndex',
                name: 'TeacherIndex',
                component: TeacherIndex
            },
        ]
    },
    {
        path: '/student',
        name: 'student',
        component: Student,
        children: [
            {
                path: '/StudentIndex',
                name: 'StudentIndex',
                component: StudentIndex
            },
        ]
    },
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
]

const router = new VueRouter({
    routes,
    mode: 'history', //去掉路由中的#
})

// to：要调整到的路由
// from：从哪个路由过来
// next对应一个匿名函数，继续往下走，可传递下一个路由参数
router.beforeEach((to, from, next) => {
    let hasRoutes = store.state.menus.hasRoutes  // 状态管理器里是否有路有信息
    let token = cookies.get("token")
    let userinfo = cookies.get("userinfo")
    if (to.path === '/login') {//不拦截登录
        next()
    } else if (!token || !userinfo) {  //token不存在，跳转到登陆页面
        next({path: '/login'})
    } else if (token && !hasRoutes) { //token存在但是hasRoute=false，根据userinfo设置路由

        axios({
            url: 'http://127.0.0.1:8000/api/v1/user/path/get_path/',
            method: 'get',
        }).then(res => {
            let all_path = res.data.path
            let newRoutes = router.options.routes
            let children_path = ''
            if (userinfo.role === 1) { // 从配置文件中加载教师的路由列表
                store.commit("setMenuList", all_path.teacher)
                children_path = all_path.teacher
                children_path.forEach(menu => {
                    if (menu.children) {
                        menu.children.forEach(child => {
                            // 转成路由
                            let route = menuToRoute(child)
                            // 把路由添加到第一个路由的children中
                            if (route) {
                                newRoutes[0].children.push(route)
                            }
                        })
                    }
                })

            } else if (userinfo.role === 0) { // 从配置文件中加载学生的路由列表
                store.commit("setMenuList", all_path.student)
                children_path = all_path.student
                children_path.forEach(menu => {
                    if (menu.children) {
                        menu.children.forEach(child => {
                            // 转成路由
                            let route = menuToRoute(child)
                            // 把路由添加到第一个路由的children中
                            if (route) {
                                newRoutes[1].children.push(route)
                            }
                        })
                    }
                })
            }
            console.log(children_path)
            // 动态绑定路由
            console.log(newRoutes)
            router.matcher = new VueRouter({mode: 'history', base: 's-connect', routes: newRoutes}).matcher;
            // router.matcher = new VueRouter({routes: newRoutes}).matcher
            // router.addRoutes(newRoutes)
            // console.log(router.getRoutes())
            let is_route = true
            store.commit("changeRouteStatus", is_route)
        })
    }
    // next({path: to.fullPath})
    next()
})


// 导航转成路由
const menuToRoute = (menu) => {
    if (!menu.component) {
        return null
    }
    let route = {
        name: menu.name,
        path: menu.path,
        meta: {
            icon: menu.icon,
            title: menu.title
        }
    }
    route.component = () => import('@/views/' + menu.component)
    return route
}

export default router
